import Vue from "vue";
import VueRouter from "vue-router";
import store from "./store";
import StorageModel from "@/utils/storage";
const Storage = new StorageModel();
import { AUTH } from "@/utils/constant";
import api, { logout } from "@/api";
// component
const Layout = () => import("./components/layout");
const Login = () => import("./views/Login");
const Home = () => import("./views/Home");
const Register = () => import("./views/Register");
const ForgetPassord = () => import("./views/ForgetPassord");
const Trade = () => import("./views/Trade");
const Order = () => import("./views/Order");
const Center = () => import("./views/Center");
const System = () => import("./views/System");
const Match = () => import("./views/Match");
const ChatRoom = () => import("./views/ChatRoom");
const Buy = () => import("./views/Buy");
const BuySuccess = () => import("./views/BuySuccess");
const Info = () => import("./views/Info");
const Level = () => import("./views/Level");
const Payee = () => import("./views/Payee");
const Identify = () => import("./views/Identify");
const SafeCenter = () => import("./views/SafeCenter"); //安全中心
const BindPhone = () => import("./views/BindPhone"); //绑定手机号
const ChangePassword = () => import("./views/ChangePassword"); //修改登录密码
const TradePassword = () => import("./views/TradePassword"); //修改交易密码

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: "",
      component: Layout,
      children: [
        {
          path: "/",
          name: "home",
          component:  Home,
        },
        {
          path: "/trade",
          name: "trade",
          component: Trade,
        },
        {
          path: "/order",
          name: "order",
          component: Order,
        },
        {
          path: "/center",
          name: "center",
          component: Center,
        },
        {
          path: "/buy",
          name: "buy",
          component: Buy,
        },
        {
          path: "/buySuccess",
          name: "buySuccess",
          component: BuySuccess,
        },

      ]
    },
    {
      path: "/login",
      name: "login",
      component: Login,
    },
    {
      path: "/register",
      name: "register",
      component: Register,
    },
    {
      path: "/forgetPassord",
      name: "forgetPassord",
      component: ForgetPassord,
    },
    {
      path: "/system",
      name: "system",
      component: System,
    },
    {
      path: "/match",
      name: "match",
      component: Match,
    },
    {
      path: "/chatRoom",
      name: "chatRoom",
      component: ChatRoom,
    },
    {
      path: "/info",
      name: "info",
      component: Info,
    },
    {
      path: "/level",
      name: "level",
      component: Level,
    },
    {
      path: "/identify",
      name: "identify",
      component: Identify,
    },
    {
      path: "/payee",
      name: "payee",
      component: Payee,
    },
    {
      path: "/safeCenter",
      name: "safeCenter",
      component: SafeCenter,
    },
    {
      path: "/bindPhone",
      name: "bindPhone",
      component: BindPhone,
    },
    {
      path: "/changePassword",
      name: "changePassword",
      component: ChangePassword,
    },
    {
      path: "/tradePassword",
      name: "tradePassword",
      component: TradePassword,
    },
    

    
    {
      path: "*",
      name: "all",
      redirect: "/"
    }
  ]
});

const whiteList = [
  "login",
  "register",
  "forgetPassord",
  "trade",
];

// 导航守卫设置请求token
router.beforeEach(async (to, from, next) => {
  const token = Storage.get(AUTH);
  if (token) {
    const userInfo = store.state.global.loginUserinfo;
    if (userInfo) {
      next();
    } else {
      try {
        api.setToken(token);
        next();
      } catch (e) {
        Storage.remove(AUTH);
        next("/");
      }
    }
  } else {
    if (to.name === "home") {
      logout();
      next();
    } else if (whiteList.indexOf(to.name) !== -1) {
      next();
    } else {
      logout();
      next("/");
    }
  }
});
export default router;
